package report

import (
	"context"
	"git.mycaigou.com/gfyx/micro-gfyx-user-service/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-user-service/infrastructure/common/utils"
	micro_gfyx_oppty_service "git.mycaigou.com/gfyx/micro-gfyx-user-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/clue"
	"time"

	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.myscrm.cn/golang/common/uuid"
	"git.myscrm.cn/golang/stark/v4"
	"gorm.io/gorm"
)

type ReportShareRepository struct {
}

func NewReportShareRepository() ReportShareRepositoryIface {
	return &ReportShareRepository{}
}

// GetInfo 获取详情
func (r *ReportShareRepository) GetInfo(ctx context.Context, condition map[string]interface{}) (*ReportShare, error) {
	db := stark.MySQLConn.Get(ctx)
	reportShare := &ReportShare{}
	err := db.Table(reportShare.TableName()).Where(condition).Where("is_deleted = 0").First(&reportShare).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return reportShare, nil
}

// GetList 获取列表数据
func (r *ReportShareRepository) GetList(ctx context.Context, condition map[string]interface{}, pageInfo *utils.PageInfo, orderBy string) (int64, []*ReportShare, error) {
	db := stark.MySQLConn.Get(ctx)

	var reportShareList []*ReportShare
	var count int64
	db = db.Table("report_share").Where(condition)
	err := db.Count(&count).Error
	if err != nil {
		return 0, nil, err
	}

	if pageInfo != nil && pageInfo.PageSize > 0 && pageInfo.Page > 0 {
		db = db.Offset((pageInfo.Page - 1) * pageInfo.PageSize).Limit(pageInfo.PageSize)
	}
	if orderBy != "" {
		db = db.Order(orderBy)
	}
	err = db.Find(&reportShareList).Error
	if err != nil {
		return 0, nil, err
	}
	return count, reportShareList, nil
}

// Update 更新数据
func (r *ReportShareRepository) Update(ctx context.Context, data, condition map[string]interface{}) (bool, error) {
	db := stark.MySQLConn.Get(ctx)
	data["modified_on"] = time.Now()
	err := db.Model(&ReportShare{}).Where(condition).Updates(data).Error
	if err != nil {
		return false, err
	}
	return true, nil
}

// Insert 插入数据
func (r *ReportShareRepository) Insert(ctx context.Context, reportShare *ReportShare) (bool, error) {
	db := stark.MySQLConn.Get(ctx)
	reportShare.Uid = uuid.GetUUID()
	reportShare.ReceiveOn = time.Unix(0, 0)
	reportShare.ModifiedOn = time.Now()
	err := db.Save(reportShare).Error
	if err != nil {
		return false, err
	}

	return true, nil
}

// Delete 逻辑删除数据
func (r *ReportShareRepository) Delete(ctx context.Context, condition map[string]interface{}) (bool, error) {
	//todo 补充逻辑
	return false, nil
}

// SendShareReceiveMsg 发送分享领取通知
func (r *ReportShareRepository) SendShareReceiveMsg(ctx context.Context, tenantCode string, projectTitle string, shareUserTenantId string, receiveMobile string) error {
	request := &micro_gfyx_oppty_service.SendShareReceiveMsgRequest{
		ProjectTitle:      projectTitle,
		ShareUserTenantId: shareUserTenantId,
		ReceiveMobile:     receiveMobile,
	}
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MicroGfyxOpptyService)
	if err != nil {
		return err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := micro_gfyx_oppty_service.NewReportServiceClient(conn)
	_, err = client.SendShareReceiveMsg(ctx, request)
	if err != nil {
		return err
	}
	return nil
}
